C++中list使用和<<重载的问题
来源:百度知道 编辑:UC知道 时间:2024/06/23 17:54:07
类Eatery中有一个list<Meal> mealList;
SnackCart公有继承自Eatery
但是在下面的函数中
ostream& operator <<(ostream& out,const SnackCart& e)
{
out<<e.owner<<endl;
list<Meal>::iterator it;
for (it=e.mealList.begin();it!=e.mealList.end();++it)
{
out<<it->getName()
<<endl<<it->getCost()
<<endl<<it->getRating()
<<endl;
}
return out;
}
for那一行报错
/home/zhaoxiang/workspace/EateryGuide/SnackCart.cpp|17|错误: no match 为‘operator=’在‘it = ((const SnackCart*)e)->SnackCart::<anonymous>.Eatery::mealList.std::list<_Tp, _Alloc>::begin [with _Tp = Meal, _Alloc = std::allocator<Meal>]()’中|
但是如果把这行改成
list<Meal> mealList=e.mealList;
for (it=mealList.begin();it!=mealList.end();++it)
就好了,但是这样就多费了一个list的空间
请问怎样才能使用类似我上面那个那样的
我的操作系统是Ubuntu9.04,IDE是Code
SnackCart公有继承自Eatery
但是在下面的函数中
ostream& operator <<(ostream& out,const SnackCart& e)
{
out<<e.owner<<endl;
list<Meal>::iterator it;
for (it=e.mealList.begin();it!=e.mealList.end();++it)
{
out<<it->getName()
<<endl<<it->getCost()
<<endl<<it->getRating()
<<endl;
}
return out;
}
for那一行报错
/home/zhaoxiang/workspace/EateryGuide/SnackCart.cpp|17|错误: no match 为‘operator=’在‘it = ((const SnackCart*)e)->SnackCart::<anonymous>.Eatery::mealList.std::list<_Tp, _Alloc>::begin [with _Tp = Meal, _Alloc = std::allocator<Meal>]()’中|
但是如果把这行改成
list<Meal> mealList=e.mealList;
for (it=mealList.begin();it!=mealList.end();++it)
就好了,但是这样就多费了一个list的空间
请问怎样才能使用类似我上面那个那样的
我的操作系统是Ubuntu9.04,IDE是Code
对, 应该使用 常量迭代器。
ostream& operator <<(ostream& out,const SnackCart& e)
你的 const SnackCart& e; 申明的是 const的,所以对应的迭代器也应该使用const_iterator;
我的操作系统也是 Ubuntu9.04
我也用CodeBlocks,不过一般的短程序就直接开VIM写了 呵呵
因为e是const的
list<Meal>::const_iterator it ; 这样试试看
getName 要声明成 const的。 所有不要改类的内容的函数都声明成const的
ostream& operator <<(ostream& out,SnackCart& e)应该这样,不要const
java中如何使用LIST的ADD方法
怎么使用laundry list和国际信用卡种类
List<string>
Java 中对list 的编列,用List.iterator() 还是用for(int i=0;i<size();i++)
vf中display和list命令有什么区别吗
java中vector和array list的区别是什么?
list<LNode>::const_iterator
apache xmlrpc 使用list问题
使用Struts+Spring+Hibernate时,怎么将页面中的一个list一次提交到Action中
用C语言编的数据结构程序中出现Argument list syntax error